﻿using System.Collections.Generic;

namespace ChessGameEngine
{
    public class Rook : Piece
    {
        
        public Rook(ChessEngine chessEngine, object uiElement, PieceColor color, TilePosition position)
            : base(chessEngine, uiElement, color)
        {
            startPosition = position;
        }

        public override IEnumerable<TilePosition> GetAllPossibleMoves()
        {
            int iStart = this.Position.I + 1;
            for (int i = iStart; i < ChessEngine.MaxTiles; i++)
            {
                var position = new TilePosition { I = i, J = this.Position.J };
                if (chessEngine.IsTileEmpty(position))
                {
                    yield return position;
                    continue;
                }

                if (chessEngine.IsOpponent(position, this.Color))
                {
                    yield return position;                    
                }
                break;
            }
            
            int jStart = this.Position.J + 1;
            for (int j = jStart; j < ChessEngine.MaxTiles; j++)
            {
                var position = new TilePosition { I = this.Position.I, J = j };
                if (chessEngine.IsTileEmpty(position))
                {
                    yield return position;
                    continue;
                }
                    
                if (chessEngine.IsOpponent(position, this.Color))
                {
                    yield return position;                    
                }
                break;
            }

            iStart = this.Position.I - 1;
            for (int i = iStart; i >= 0; i--)
            {
                var position = new TilePosition { I = i, J = this.Position.J };
                if (chessEngine.IsTileEmpty(position))
                {
                    yield return position;
                    continue;
                }
                    
                if(chessEngine.IsOpponent(position, this.Color))
                {
                    yield return position;                    
                }
                break;
            }

            jStart = this.Position.J - 1;
            for (int j = jStart; j >= 0; j--)
            {
                var position = new TilePosition { I = this.Position.I, J = j };
                if (chessEngine.IsTileEmpty(position))
                {
                    yield return position;
                    continue;
                }

                if (chessEngine.IsOpponent(position, this.Color))
                {
                    yield return position;                    
                }
                break;
            }

/*
            var king = chessEngine.KingOfSelectedPiece;

            if (!Moved && !king.Moved && !king.Check)
            {
                bool queenSideCastling = this.Position.I == 0;
                bool cleanRange = true;
                var pos = new TilePosition { J = this.Position.J };
                if (queenSideCastling)
                {
                    for (int i = 1; i < king.Position.I && cleanRange; i++)
                    {
                        pos.I = i;
                        cleanRange &= chessEngine.IsTileClean(pos);
                    }
                }
                else if (this.Position.I == 7) // kingSideCastling
                {
                    for (int i = 7; i > king.Position.I && cleanRange; i--)
                    {
                        pos.I = i;
                        cleanRange &= chessEngine.IsTileClean(pos);
                    }                    
                }
                if (cleanRange)
                    yield return pos;

            }
*/
        }
    }
}
